package msu.ml.data;

import weka.core.*;
import weka.classifiers.*;
import msu.ml.gis.*;
import msu.ml.core.*;
import java.util.*;
import java.io.*;

/**
 * This class has not yet been implemented.
 *
 * @author Reginald M Mead
 * @version 1.0
 */
public class CSVPostProcessor extends DataPostProcessorAdapter
{
    private ArrayList<String> indices;

    public CSVPostProcessor()
    {
        this.indices = new ArrayList<String>();
    }

    public void setIndices(String indiceString)
    {
        indices.clear();
        for(String ind : indiceString.split(",\\s*"))
            indices.add(ind);
    }

    public ArrayList<String> getIndices()
    {
        return indices;
    }

	public void endExperiment()
	{
		Experiment exp = getExperiment();
		Classifier c = exp.getClassifier();
		IDataProvider p = exp.getDataProvider();

		if(p instanceof DbCrossValidationDataProvider)
		{
			DbCrossValidationDataProvider dbp = 
				(DbCrossValidationDataProvider)p;

			System.out.println("Creating CSV Files...");

			HashSet<String> fullFeatures = new HashSet<String>(exp.getFeatures());
            ArrayList<String> csvFeatures = new ArrayList<String>();
            for(String indice : this.indices)
            {
                if(!fullFeatures.contains(indice))
                {
                    fullFeatures.add(indice);
                    csvFeatures.add(indice);
                }
            }

			AttributeRemovalPreProcessor filter = new
				AttributeRemovalPreProcessor();
			filter.setInvertSelection(true);
			String featureString = fullFeatures.toString();
			filter.setAttributeIndices(featureString.substring(1,
						featureString.length() - 1));

			ArrayList<IDataPreProcessor> filters = new
				ArrayList<IDataPreProcessor>();
			filters.add(filter);
			DatabaseCache cache = dbp.getCache();

			File imageDir = new File(exp.getName() + "_csv");
			imageDir.mkdir();

			for(String file : dbp.getFileNames())
			{
				System.out.println(file + ".csv");

				try
				{

					NxInstances richData = cache.retrieve(file, filters);
					NxInstances data = new NxInstances(richData);
                    int indx = 0;
                    for(String indice : filter.getAttributeIndices().split(","))
                    {
                        if(csvFeatures.contains(indice))
                            data.deleteAttributeAt(indx);
                            
                        indx++;
                    }

                    PrintWriter sout = new PrintWriter(imageDir.getName() + 
                        File.separatorChar + file + ".csv");

					for(int i = 0; i < data.numInstances(); i++)
					{
						double [] dist = c.distributionForInstance(data.instance(i));
						Instance inst = richData.instance(i);

                        sout.println(String.format("%1$.1f, %2$d, %3$.4f",
                            inst.value(0), (int)(inst.value(1)), dist[1]));
					}

                    sout.close();

				}
				catch(Exception e)
				{
					System.out.println(e);
					e.printStackTrace();
				}
			}
		}
	}
}
